package com.bh.web.controller.extend;

import com.bh.common.annotation.Log;
import com.bh.common.constant.Symbol;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.StringUtils;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.kysys.service.IJcZoneService;
import com.bh.order.domain.JcServerZone;
import com.bh.order.service.IJcServerZoneService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 服务区域Controller
 *
 * @author slx
 * @date 2023-05-05
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("/extend/serverZone")
public class JcServerZoneController extends BaseController {

    @Autowired
    private IJcServerZoneService jcServerZoneService;

    @Autowired
    private IJcZoneService jcZoneService;

    /**
     * 修改服务区域
     */
    @GetMapping("/getZoneByOrgId/{orgId}")
    public AjaxResult getZoneByOrgId(@PathVariable("orgId") Long orgId) {
        if (orgId == null || orgId == 0L) {
            orgId = getOrgId();
        }
        List<JcServerZone> zones = jcServerZoneService.selectJcServerZoneByOrgId(orgId);
        return AjaxResult.success(zones);
    }


    /**
     * 查询服务区域列表
     */
    @ApiOperation("查询服务区域列表")
    @PreAuthorize("@ss.hasPermi('extend:serverZone:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcServerZone jcServerZone) {
        jcServerZone.setJcOrganizationId(getOrgId());
        startPage();
        List<JcServerZone> list = jcServerZoneService.selectJcServerZoneList(jcServerZone);
        return getDataTable(list);
    }

    /**
     * 导出服务区域列表
     */
    @PreAuthorize("@ss.hasPermi('extend:serverZone:export')")
    @Log(title = "服务区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcServerZone jcServerZone) {
        List<JcServerZone> list = jcServerZoneService.selectJcServerZoneList(jcServerZone);
        ExcelUtil<JcServerZone> util = new ExcelUtil<JcServerZone>(JcServerZone.class);
        util.exportExcel(response, list, "服务区域数据");
    }


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('extend:serverZone:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<JcServerZone> util = new ExcelUtil<>(JcServerZone.class);
        List<JcServerZone> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = jcServerZoneService.importData(userList, operName);
        return success(message);
    }

    /**
     * 获取服务区域详细信息
     */
    @ApiOperation("获取服务区域详细信息")
    @PreAuthorize("@ss.hasPermi('extend:serverZone:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jcServerZoneService.selectJcServerZoneById(id));
    }

    /**
     * 新增服务区域
     */
    @ApiOperation("新增服务区域")
    @PreAuthorize("@ss.hasPermi('extend:serverZone:add')")
    @Log(title = "服务区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcServerZone jcServerZone) {
        jcServerZone.setJcOrganizationId(getOrgId());
        if (StringUtils.isNotEmpty(jcServerZone.getJcZones()) && StringUtils.isNotEmpty(jcServerZone.getJcZoneIds())) {
            String[] jcZonesArrays = jcServerZone.getJcZones().split(Symbol.HYPHEN);
            String[] jcZoneIdArrays = jcServerZone.getJcZoneIds().split(Symbol.HYPHEN);
            if (jcZonesArrays.length > 1 && jcZoneIdArrays.length > 0) {
                int num = 0;
                for (int i = 0; i < jcZonesArrays.length; i++) {
                    jcServerZone.setId(null);
                    jcServerZone.setJcZones(jcZonesArrays[i]);
                    jcServerZone.setJcZoneId(Long.valueOf(jcZoneIdArrays[i]));
                    String jcZoneNames = jcZoneService.selectJcZoneNamesByIds(jcZonesArrays[i].split(","));
                    if (StringUtils.isNotBlank(jcZoneNames)) {
                        jcServerZone.setName(jcZoneNames);
                    }
                    num += jcServerZoneService.insertJcServerZone(jcServerZone);
                }
                return toAjax(num);
            } else {
                return toAjax(jcServerZoneService.insertJcServerZone(jcServerZone));
            }
        }
        return error();
    }

    /**
     * 修改服务区域
     */
    @ApiOperation("修改服务区域")
    @PreAuthorize("@ss.hasPermi('extend:serverZone:edit')")
    @Log(title = "服务区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcServerZone jcServerZone) {
        jcServerZone.setJcOrganizationId(getOrgId());
        return toAjax(jcServerZoneService.updateJcServerZone(jcServerZone));
    }

    /**
     * 删除服务区域
     */
    @ApiOperation("删除服务区域")
    @PreAuthorize("@ss.hasPermi('extend:serverZone:remove')")
    @Log(title = "服务区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jcServerZoneService.deleteJcServerZoneByIds(ids));
    }


    /**
     * 获取服务区域价格
     */
    @ApiOperation("获取服务区域价格")
    @GetMapping("/getZonePrice/{zoneId}")
    public AjaxResult getZonePrice(@PathVariable("zoneId") String zoneId) {
        return success(jcServerZoneService.getZonePrice(zoneId));
    }

}
